// 路由鉴权
import router from "./router";
// 引入进度条
import Nprogress from 'nprogress'
// 引入进度条样式
import 'nprogress/nprogress.css'
//引入用户相关的仓库
import useUserStore from '@/store/modules/user';
// 引入大仓库
import pinia from '@/store'
let userStore = useUserStore(pinia);

// 存储用户未登录可以访问路由的路径
let whiteList = ['/home','/hospital/register','/hospital/detail','/hospital/notice','/hospital/close','/hospital/search']
// 前置路由守卫
router.beforeEach((to,_,next)=>{
    // 访问路由组件之前，进度条开始动
    Nprogress.start()
    // 动态设置网页左上角的标题
    document.title = `尚医通-${to.meta.title}`
    // 判断用户是否登录-携带token
    let token = userStore.userInfo.token
    // 用户登录了
    if(token){
        next()
    } else{
        // 用户未登录
        if(whiteList.includes(to.path)){
            next()
        } else {
            userStore.visiable = true
            next({path:'/home',query:{redirect:to.fullPath}})
        }
    }
})

// 后置路由守卫
router.afterEach((to,from)=>{
    Nprogress.done()
})